﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Drawing.Imaging;

namespace FF_Plugin
{
    public delegate bool CallBack(int hwnd, int y);
    public partial class FF_Plugin : Form
    {
        private string mWndname = "大家来找茬";
        private int mHwnd = -1;
        private Bitmap mImage = null;
        private bool mFound = false;
        private Rectangle mRectLeft = new Rectangle(8, 191,498, 448);
        private Rectangle mRectRight = new Rectangle(516, 191, 498, 448);
        public FF_Plugin()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        [DllImport("user32.dll")]
        public static extern int EnumWindows(CallBack x, int y);
        [DllImport("user32")]
        public static extern int GetWindowText(int hwnd, StringBuilder lptrString, int nMaxCount);
        [DllImport("user32")]
        public static extern int GetParent(int hwnd);
        [DllImport("user32")]
        public static extern int IsWindowVisible(int hwnd);

        /// 
        /// Helper class containing Gdi32 API functions
        /// 
        private class GDI32
        {

            public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
            [DllImport("gdi32.dll")]
            public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
                int nWidth, int nHeight, IntPtr hObjectSource,
                int nXSrc, int nYSrc, int dwRop);
            [DllImport("gdi32.dll")]
            public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
                int nHeight);
            [DllImport("gdi32.dll")]
            public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
            [DllImport("gdi32.dll")]
            public static extern bool DeleteDC(IntPtr hDC);
            [DllImport("gdi32.dll")]
            public static extern bool DeleteObject(IntPtr hObject);
            [DllImport("gdi32.dll")]
            public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
        }

        /// 
        /// Helper class containing User32 API functions
        /// 
        private class User32
        {
            [StructLayout(LayoutKind.Sequential)]
            public struct RECT
            {
                public int left;
                public int top;
                public int right;
                public int bottom;
            }
            [DllImport("user32.dll")]
            public static extern IntPtr GetDesktopWindow();
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowDC(IntPtr hWnd);
            [DllImport("user32.dll")]
            public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
        }

        /// 
        /// Creates an Image object containing a screen shot of a specific window?
        /// 
        /// The handle to the window. (In windows forms, this is obtained by the Handle property)
        /// 
        public Image CaptureWindow(IntPtr handle)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);
            // get the size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            int width = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;
            // create a device context we can copy to
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
            // select the bitmap object
            IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
            // bitblt over
            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
            // restore selection
            GDI32.SelectObject(hdcDest, hOld);
            // clean up 
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);
            // get a .NET image object for it
            Image img = Image.FromHbitmap(hBitmap);
            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);
            return img;
        }



         private void btCheat_Click(object sender, EventArgs e)
        {
            Process[] ProcArray = Process.GetProcesses();
            mHwnd = -1;
             EnumWindows(this.Find, 0);

             mFound = false;
            
            //found the wnd.
             if (-1 != mHwnd)
             {
                 mImage = new Bitmap(CaptureWindow((IntPtr)mHwnd));
                 FindAndMarkDifferent(mImage);
                 Point pt = new Point(0, 0);
                 Graphics g = this.CreateGraphics();
                 g.DrawImage(mImage, pt);
             }
             else 
             {
                 MessageBox.Show("You Haven't Open the Program yet, Don't be joke!");
             }
        }

        private bool Find(int hwnd, int lParam)
        {
            int pHwnd;
            pHwnd = GetParent(hwnd);

            if (pHwnd == 0 && IsWindowVisible(hwnd) == 1)
            {
                StringBuilder name = new StringBuilder(512);

                GetWindowText(hwnd, name, name.Capacity);
                if ((mWndname == name.ToString()) && !mFound)
                {
                    //Debug.WriteLine(name.ToString());
                    mHwnd = hwnd;
                    mFound = true;
                }
            }
            return true;
        }

        private void FindAndMarkDifferent(Bitmap image)
        {
            // Loop through the images pixels to reset color.
            for (int x = mRectLeft.X; x < mRectLeft.Width + mRectLeft.X; x++)
            {
                for (int y = mRectLeft.Y; y < mRectLeft.Height+mRectLeft.Y; y++)
                {
                    Color pixelColorLeft = image.GetPixel(x, y);
                    Color pixelColorRight = image.GetPixel(x+(mRectRight.X - mRectLeft.X)+1, y);
                    if (pixelColorLeft != pixelColorRight)
                    {
                        Color newColor = Color.FromArgb(255, 0, 0);
                        image.SetPixel(x, y, newColor);   
                    }
                }
            }

        }

    }
}
